package com.qqtech.qquan.im.controller;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.qqtech.core.common.annotation.VisitAnnt;
import com.qqtech.core.common.enums.RespEnum;
import com.qqtech.core.common.enums.YesNoEnum;
import com.qqtech.core.common.util.StringUtil;
import com.qqtech.core.frame.model.PageKit;
import com.qqtech.core.frame.model.PageOrder;
import com.qqtech.core.frame.model.Result;
import com.qqtech.core.frame.service.BaseService;
import com.qqtech.qquan.im.enums.ImUserStatusEnum;
import com.qqtech.qquan.im.model.ImUser;
import com.qqtech.qquan.im.model.ImUserQuery;
import com.qqtech.qquan.im.model.ImUserVo;
import com.qqtech.qquan.im.service.ImUserService;
import com.qqtech.qquan.sms.enums.SmsConst;
import com.qqtech.qquan.sms.model.SmsSysParam;
import com.qqtech.qquan.sms.service.SmsSysParamService;
import com.qqtech.qquan.user.model.UserBaseQuery;
import com.qqtech.qquan.user.model.UserBaseVo;
import com.qqtech.qquan.user.service.UserBaseService;
import com.qqtech.web.context.BaseWebController;

@Controller
@RequestMapping(value = "/im/user")
public class ImUserController extends BaseWebController<ImUser, ImUserQuery> {
	@Resource
	private ImUserService imUserService;

	@Resource
	private UserBaseService userBaseService;

	@Resource
	private SmsSysParamService smsSysParamService;

	@Override
	protected BaseService<ImUser> getBaseService() {
		return imUserService;
	}

	/**
	 * 初始化用户到环信
	 * 
	 * @return
	 */
	@RequestMapping(value = "/initImUser2HuanXin")
	@ResponseBody
	public Result initImUser2HuanXin() {
		Result result = new Result();
		int addCount = 0;
		UserBaseQuery userQ = new UserBaseQuery();
		int userCount = userBaseService.queryCount(userQ);
		int pageCount = (userCount / 30) + 1;// 总页数
		for (int i = 1; i <= pageCount; i++) {
			Pageable pageKit = new PageKit(i, 30, new PageOrder(Direction.ASC, "id"));
			List<UserBaseVo> userList = userBaseService.queryList(userQ, pageKit);
			if (userList != null && userList.size() > 0) {
				for (UserBaseVo user : userList) {
					int imuserId = imUserService.addImUser(user.getId(), user.getNick());
					if (imuserId > 0) {
						addCount++;
					}
				}
			}
		}
		result.put("addMsg", "本次添加im用户：" + addCount + "个");
		return result;
	}

	/**
	 * 批量同步im_user到环信服务器
	 * 
	 * @return
	 */
	@RequestMapping(value = "/syncImUser2HuanXinBatch")
	@ResponseBody
	public Result syncImUser2HuanXinBatch() {
		Result result = new Result();
		int syncCount = 0;

		StringBuffer syncRet = new StringBuffer();
		ImUserQuery userQ = new ImUserQuery();
		int userCount = imUserService.queryCount(userQ);
		int pageCount = (userCount / 30) + 1;// 总页数
		for (int i = 1; i <= pageCount; i++) {
			Pageable pageKit = new PageKit(i, 30, new PageOrder(Direction.ASC, "userId"));
			List<ImUserVo> imUserList = imUserService.queryList(userQ, pageKit);
			if (imUserList != null && imUserList.size() > 0) {
				for (ImUserVo imUser : imUserList) {
					int retCode = imUserService.syncImUser2HuanXin(imUser.getImId(), imUser.getImPassWord(),
							imUser.getImNickName());
					if (retCode > 0) {
						syncCount++;
						syncRet.append(imUser.getImNickName() + ",");
					}
				}
			}
		}
		result.put("syncImUser", "本次同步im_user用户到环信服务器,更新：" + syncCount + "个:\n" + syncRet.toString());
		return result;
	}

	/**
	 * 查询用户的im信息 用于跟他/她聊天
	 * 
	 * @return
	 */
	@VisitAnnt(type = RespEnum.LOGIN)
	@RequestMapping(value = "/getImUser")
	@ResponseBody
	public Result getImUser(@RequestBody ImUserQuery imUserQ) {
		Result result = new Result();
		Integer userId = imUserQ.getUserId();
		String imId = imUserQ.getImId();
		UserBaseVo userVo = null;
		ImUserVo imUser = null;
		if (userId != null) {
			userVo = userBaseService.getById(userId);
			imUser = imUserService.getByUserId(userId);
		} else if (imId != null) {

			imUser = imUserService.getByImId(imId);
			if (imUser != null && imUser.getUserId() != null) {
				userVo = userBaseService.getById(imUser.getUserId());
			}
		} else {
			result.addErr("用户信息userId/imId为空");
			return result;
		}

		if (userVo != null && imUser != null) {
			imUser.setLogo(
					StringUtil.isNotBlank(userVo.getLogo()) ? userVo.getLogo() : "source://image/common/userLogin.png");
			imUser.setImNickName(StringUtil.isNotBlank(userVo.getNick()) ? userVo.getNick() : "新手");
		}

		result.put("imUser", imUser);
		return result;
	}

	/**
	 * 查询我的im用户信息
	 * 
	 * @return
	 */
	@VisitAnnt(type = RespEnum.LOGIN)
	@RequestMapping(value = "/getMyImUser")
	@ResponseBody
	public Result getMyImUser() {
		Result result = new Result();
		Integer userId = super.getUserId();
		if (userId == null) {
			result.addErr("用户id为空！");
			return result;
		}
		UserBaseVo userVo = userBaseService.getById(userId);
		ImUserVo imUser = imUserService.getByUserId(userId);
		if (userVo != null && imUser != null) {
			imUser.setLogo(
					StringUtil.isNotBlank(userVo.getLogo()) ? userVo.getLogo() : "source://image/common/userLogin.png");
			imUser.setImNickName(StringUtil.isNotBlank(userVo.getNick()) ? userVo.getNick() : "新手");
		}
		String isEnableIosChat = "" + YesNoEnum.NO.getCode();
		SmsSysParam smsSysParam = smsSysParamService.getByKey(SmsConst.PARAMKEY_SMS_ENABLE_IOSCHAT);
		if (smsSysParam != null && StringUtil.isNotBlank(smsSysParam.getValue())) {
			isEnableIosChat = smsSysParam.getValue();
		}
		result.put("isEnableIosChat", isEnableIosChat);
		result.put("myImUser", imUser);
		return result;
	}

	@VisitAnnt(type = RespEnum.LOGIN)
	@RequestMapping(value = "/isMeOnline")
	@ResponseBody
	public Result isMeOnline() {
		Result result = new Result();
		Integer userId = super.getUserId();
		if (userId == null) {
			result.addErr("用户id为空！");
			return result;
		}
		ImUserStatusEnum imUserStatus = imUserService.getImUserStatus(userId);
		boolean isOnLine = false;
		if (imUserStatus.equals(ImUserStatusEnum.ONLINE)) {
			isOnLine = true;
		}
		result.put("onLine", isOnLine);
		return result;
	}

	/**
	 * 强制下线
	 * 
	 * @param imUserQ
	 * @return
	 */
	@RequestMapping(value = "/offLine")
	@ResponseBody
	public Result offLine(@RequestBody ImUserQuery imUserQ) {
		Result result = new Result();
		String userImId = imUserQ.getImId();
		if (userImId == null) {
			result.addErr("用户imid为空！");
			return result;
		}
		boolean isSuccess = imUserService.forceImUserOffLine(userImId);
		result.put("isSuccess", isSuccess);
		return result;
	}

	/**
	 * 查询在线客服
	 * 
	 * @return 一个客服人员
	 */
	@RequestMapping(value = "/getImOnlineService")
	@ResponseBody
	public Result getImOnlineService() {
		Result result = new Result();
		UserBaseQuery user = new UserBaseQuery();
		user.setIsOnlineService(YesNoEnum.YES.getCode());
		Pageable pageKit = new PageKit(1, 30, new PageOrder(Direction.ASC, "id"));
		List<UserBaseVo> userList = userBaseService.queryList(user, pageKit);
		if (userList != null && userList.size() > 0) {
			result.put("imOnlineService", userList.get(0));
		} else {
			result.addErr("当前在线客服繁忙，请稍候再试。");
		}

		return result;
	}
}
